{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "import pandas as pd\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "***** xtdata连接成功 *****\n",
      "服务信息: None\n",
      "服务地址: None\n",
      "数据路径: None\n",
      "设置xtdata.enable_hello = False可隐藏此消息\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'timetag': '20240814 15:00:03',\n",
       " 'lastPrice': 1413.3,\n",
       " 'open': 1423.01,\n",
       " 'high': 1424.9,\n",
       " 'low': 1412.02,\n",
       " 'lastClose': 1423.01,\n",
       " 'amount': 1898377400,\n",
       " 'volume': 13414,\n",
       " 'pvolume': 1341431,\n",
       " 'stockStatus': 0,\n",
       " 'openInt': 15,\n",
       " 'settlementPrice': 0,\n",
       " 'lastSettlementPrice': 0,\n",
       " 'askPrice': [1413.3, 1413.31, 1413.32, 0, 0],\n",
       " 'bidPrice': [1413.28, 1413.26, 1413.24, 0, 0],\n",
       " 'askVol': [39, 2, 1, 0, 0],\n",
       " 'bidVol': [2, 1, 1, 0, 0]}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res = xtdata.get_full_tick(['600519.SH'])\n",
    "\n",
    "res['600519.SH']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'600519.SH': {'time': 1723618803000, 'lastPrice': 1413.3, 'open': 1423.01, 'high': 1424.9, 'low': 1412.02, 'lastClose': 1423.01, 'amount': 1898377400.0, 'volume': 13414, 'pvolume': 1341431, 'stockStatus': 0, 'openInt': 15, 'transactionNum': 0, 'lastSettlementPrice': 0.0, 'settlementPrice': 0.0, 'pe': 0.0, 'askPrice': [1413.3, 1413.31, 1413.32, 0.0, 0.0], 'bidPrice': [1413.28, 1413.26, 1413.24, 0.0, 0.0], 'askVol': [39, 2, 1, 0, 0], 'bidVol': [2, 1, 1, 0, 0], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}}\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 8\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[38;5;28mprint\u001b[39m(datas)\n\u001b[0;32m      6\u001b[0m seq \u001b[38;5;241m=\u001b[39m xtdata\u001b[38;5;241m.\u001b[39msubscribe_whole_quote(code_list\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m600519.SH\u001b[39m\u001b[38;5;124m'\u001b[39m], callback\u001b[38;5;241m=\u001b[39mon_data)\n\u001b[1;32m----> 8\u001b[0m \u001b[43mxtdata\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\users\\aaa\\miniconda3\\envs\\python39\\lib\\site-packages\\xtquant\\xtdata.py:1067\u001b[0m, in \u001b[0;36mrun\u001b[1;34m()\u001b[0m\n\u001b[0;32m   1065\u001b[0m client \u001b[38;5;241m=\u001b[39m get_client()\n\u001b[0;32m   1066\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m-> 1067\u001b[0m     \u001b[43mtime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msleep\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1068\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m client\u001b[38;5;241m.\u001b[39mis_connected():\n\u001b[0;32m   1069\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m行情服务连接断开\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "def on_data (datas):\n",
    "    print(datas)\n",
    "\n",
    "seq = xtdata.subscribe_whole_quote(code_list=['600519.SH'], callback=on_data)\n",
    "\n",
    "xtdata.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-08-14 15:00:03.000000\n",
      "2024-08-14 15:24:25.114630\n",
      "{'600519.SH': {'time': 1723618803000, 'lastPrice': 1413.3, 'open': 1423.01, 'high': 1424.9, 'low': 1412.02, 'lastClose': 1423.01, 'amount': 1898377400.0, 'volume': 13414, 'pvolume': 1341431, 'stockStatus': 0, 'openInt': 15, 'transactionNum': 0, 'lastSettlementPrice': 0.0, 'settlementPrice': 0.0, 'pe': 0.0, 'askPrice': [1413.3, 1413.31, 1413.32, 0.0, 0.0], 'bidPrice': [1413.28, 1413.26, 1413.24, 0.0, 0.0], 'askVol': [39, 2, 1, 0, 0], 'bidVol': [2, 1, 1, 0, 0], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}}\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 18\u001b[0m\n\u001b[0;32m     14\u001b[0m     \u001b[38;5;28mprint\u001b[39m(datas)\n\u001b[0;32m     16\u001b[0m seq \u001b[38;5;241m=\u001b[39m xtdata\u001b[38;5;241m.\u001b[39msubscribe_whole_quote(code_list\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m600519.SH\u001b[39m\u001b[38;5;124m'\u001b[39m], callback\u001b[38;5;241m=\u001b[39mon_data)\n\u001b[1;32m---> 18\u001b[0m \u001b[43mxtdata\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\users\\aaa\\miniconda3\\envs\\python39\\lib\\site-packages\\xtquant\\xtdata.py:1067\u001b[0m, in \u001b[0;36mrun\u001b[1;34m()\u001b[0m\n\u001b[0;32m   1065\u001b[0m client \u001b[38;5;241m=\u001b[39m get_client()\n\u001b[0;32m   1066\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m-> 1067\u001b[0m     \u001b[43mtime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msleep\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1068\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m client\u001b[38;5;241m.\u001b[39mis_connected():\n\u001b[0;32m   1069\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m行情服务连接断开\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "from datetime import datetime\n",
    "import time\n",
    "\n",
    "def on_data (datas):\n",
    "    tick_time = datas['600519.SH']['time']\n",
    "    timestamp_seconds = tick_time / 1000\n",
    "    readable_time = datetime.fromtimestamp(timestamp_seconds).strftime('%Y-%m-%d %H:%M:%S.%f')\n",
    "    # 获取当前时间戳（秒级）\n",
    "    current_timestamp_seconds = time.time()\n",
    "    current_readable_time = datetime.fromtimestamp(current_timestamp_seconds).strftime('%Y-%m-%d %H:%M:%S.%f')\n",
    "    print(readable_time)\n",
    "    print(current_readable_time)\n",
    "    print(datas)\n",
    "\n",
    "seq = xtdata.subscribe_whole_quote(code_list=['600519.SH'], callback=on_data)\n",
    "\n",
    "xtdata.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "***** xtdata连接成功 *****\n",
      "服务信息: None\n",
      "服务地址: None\n",
      "数据路径: None\n",
      "设置xtdata.enable_hello = False可隐藏此消息\n",
      "\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 18\u001b[0m\n\u001b[0;32m     14\u001b[0m     \u001b[38;5;28mprint\u001b[39m(datas)\n\u001b[0;32m     16\u001b[0m seq \u001b[38;5;241m=\u001b[39m xtdata\u001b[38;5;241m.\u001b[39msubscribe_quote(stock_code\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m600519.SH\u001b[39m\u001b[38;5;124m'\u001b[39m, period\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m1m\u001b[39m\u001b[38;5;124m'\u001b[39m, callback\u001b[38;5;241m=\u001b[39mon_data)\n\u001b[1;32m---> 18\u001b[0m \u001b[43mxtdata\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     20\u001b[0m xtdata\u001b[38;5;241m.\u001b[39munsubscribe_quote(seq)\n\u001b[0;32m     22\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m取消订阅成功\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "File \u001b[1;32md:\\users\\aaa\\miniconda3\\envs\\python39\\lib\\site-packages\\xtquant\\xtdata.py:1067\u001b[0m, in \u001b[0;36mrun\u001b[1;34m()\u001b[0m\n\u001b[0;32m   1065\u001b[0m client \u001b[38;5;241m=\u001b[39m get_client()\n\u001b[0;32m   1066\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m-> 1067\u001b[0m     \u001b[43mtime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msleep\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1068\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m client\u001b[38;5;241m.\u001b[39mis_connected():\n\u001b[0;32m   1069\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m行情服务连接断开\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "from datetime import datetime\n",
    "import time\n",
    "\n",
    "def on_data(datas):\n",
    "    tick_time = datas['600519.SH'][0]['time']\n",
    "    timestamp_seconds = tick_time / 1000\n",
    "    readable_time = datetime.fromtimestamp(timestamp_seconds).strftime('%Y-%m-%d %H:%M:%S.%f')\n",
    "    # 获取当前时间戳（秒级）\n",
    "    current_timestamp_seconds = time.time()\n",
    "    current_readable_time = datetime.fromtimestamp(current_timestamp_seconds).strftime('%Y-%m-%d %H:%M:%S.%f')\n",
    "    print(readable_time)\n",
    "    print(current_readable_time)\n",
    "    print(datas)\n",
    "\n",
    "seq = xtdata.subscribe_quote(stock_code='600519.SH', period='tick', callback=on_data)\n",
    "\n",
    "xtdata.run()\n",
    "\n",
    "xtdata.unsubscribe_quote(seq)\n",
    "\n",
    "print('取消订阅成功')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "***** xtdata连接成功 *****\n",
      "服务信息: None\n",
      "服务地址: None\n",
      "数据路径: None\n",
      "设置xtdata.enable_hello = False可隐藏此消息\n",
      "\n",
      "{'600519.SH': {'time': 1723618803000, 'lastPrice': 1413.3, 'open': 1423.01, 'high': 1424.9, 'low': 1412.02, 'lastClose': 1423.01, 'amount': 1898377400.0, 'volume': 13414, 'pvolume': 1341431, 'stockStatus': 0, 'openInt': 15, 'transactionNum': 0, 'lastSettlementPrice': 0.0, 'settlementPrice': 0.0, 'pe': 0.0, 'askPrice': [1413.3, 1413.31, 1413.32, 0.0, 0.0], 'bidPrice': [1413.28, 1413.26, 1413.24, 0.0, 0.0], 'askVol': [39, 2, 1, 0, 0], 'bidVol': [2, 1, 1, 0, 0], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}}\n",
      "取消订阅成功\n"
     ]
    }
   ],
   "source": [
    "# from xtquant import xtdata\n",
    "# import time\n",
    "\n",
    "# def on_data (datas):\n",
    "#     print(datas)\n",
    "\n",
    "# seq = xtdata.subscribe_whole_quote(code_list=['600519.SH'], callback=on_data)\n",
    "\n",
    "# time.sleep(5)\n",
    "\n",
    "# xtdata.unsubscribe_quote(seq)\n",
    "\n",
    "# print('取消订阅成功')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_df = {'600519.SH': {'time': 1723532401000, 'lastPrice': 1423.01, 'open': 1433.0, 'high': 1435.0, 'low': 1412.01,\n",
    "                          'lastClose': 1436.1, 'amount': 2317037800.0, 'volume': 16308, 'pvolume': 1630843, \n",
    "                          'stockStatus': 0, 'openInt': 15, 'transactionNum': 0, 'lastSettlementPrice': 0.0, 'settlementPrice': 0.0, \n",
    "                          'pe': 0.0, 'askPrice': [1425.0, 1425.15, 1425.2, 0.0, 0.0], 'bidPrice': [1423.01, 1423.0, 1422.99, 0.0, 0.0], \n",
    "                          'askVol': [12, 4, 1, 0, 0], 'bidVol': [2, 40, 2, 0, 0], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "keys_  = list(dict_df.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time': 1723532401000,\n",
       " 'lastPrice': 1423.01,\n",
       " 'open': 1433.0,\n",
       " 'high': 1435.0,\n",
       " 'low': 1412.01,\n",
       " 'lastClose': 1436.1,\n",
       " 'amount': 2317037800.0,\n",
       " 'volume': 16308,\n",
       " 'pvolume': 1630843,\n",
       " 'stockStatus': 0,\n",
       " 'openInt': 15,\n",
       " 'transactionNum': 0,\n",
       " 'lastSettlementPrice': 0.0,\n",
       " 'settlementPrice': 0.0,\n",
       " 'pe': 0.0,\n",
       " 'askPrice': [1425.0, 1425.15, 1425.2, 0.0, 0.0],\n",
       " 'bidPrice': [1423.01, 1423.0, 1422.99, 0.0, 0.0],\n",
       " 'askVol': [12, 4, 1, 0, 0],\n",
       " 'bidVol': [2, 40, 2, 0, 0],\n",
       " 'volRatio': 0.0,\n",
       " 'speed1Min': 0.0,\n",
       " 'speed5Min': 0.0}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_df[keys_[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt_df = pd.DataFrame(dict_df[keys_[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>lastPrice</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>lastClose</th>\n",
       "      <th>amount</th>\n",
       "      <th>volume</th>\n",
       "      <th>pvolume</th>\n",
       "      <th>stockStatus</th>\n",
       "      <th>...</th>\n",
       "      <th>lastSettlementPrice</th>\n",
       "      <th>settlementPrice</th>\n",
       "      <th>pe</th>\n",
       "      <th>askPrice</th>\n",
       "      <th>bidPrice</th>\n",
       "      <th>askVol</th>\n",
       "      <th>bidVol</th>\n",
       "      <th>volRatio</th>\n",
       "      <th>speed1Min</th>\n",
       "      <th>speed5Min</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1723532401000</td>\n",
       "      <td>1423.01</td>\n",
       "      <td>1433.0</td>\n",
       "      <td>1435.0</td>\n",
       "      <td>1412.01</td>\n",
       "      <td>1436.1</td>\n",
       "      <td>2.317038e+09</td>\n",
       "      <td>16308</td>\n",
       "      <td>1630843</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1425.00</td>\n",
       "      <td>1423.01</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1723532401000</td>\n",
       "      <td>1423.01</td>\n",
       "      <td>1433.0</td>\n",
       "      <td>1435.0</td>\n",
       "      <td>1412.01</td>\n",
       "      <td>1436.1</td>\n",
       "      <td>2.317038e+09</td>\n",
       "      <td>16308</td>\n",
       "      <td>1630843</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1425.15</td>\n",
       "      <td>1423.00</td>\n",
       "      <td>4</td>\n",
       "      <td>40</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1723532401000</td>\n",
       "      <td>1423.01</td>\n",
       "      <td>1433.0</td>\n",
       "      <td>1435.0</td>\n",
       "      <td>1412.01</td>\n",
       "      <td>1436.1</td>\n",
       "      <td>2.317038e+09</td>\n",
       "      <td>16308</td>\n",
       "      <td>1630843</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1425.20</td>\n",
       "      <td>1422.99</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1723532401000</td>\n",
       "      <td>1423.01</td>\n",
       "      <td>1433.0</td>\n",
       "      <td>1435.0</td>\n",
       "      <td>1412.01</td>\n",
       "      <td>1436.1</td>\n",
       "      <td>2.317038e+09</td>\n",
       "      <td>16308</td>\n",
       "      <td>1630843</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1723532401000</td>\n",
       "      <td>1423.01</td>\n",
       "      <td>1433.0</td>\n",
       "      <td>1435.0</td>\n",
       "      <td>1412.01</td>\n",
       "      <td>1436.1</td>\n",
       "      <td>2.317038e+09</td>\n",
       "      <td>16308</td>\n",
       "      <td>1630843</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            time  lastPrice    open    high      low  lastClose        amount  \\\n",
       "0  1723532401000    1423.01  1433.0  1435.0  1412.01     1436.1  2.317038e+09   \n",
       "1  1723532401000    1423.01  1433.0  1435.0  1412.01     1436.1  2.317038e+09   \n",
       "2  1723532401000    1423.01  1433.0  1435.0  1412.01     1436.1  2.317038e+09   \n",
       "3  1723532401000    1423.01  1433.0  1435.0  1412.01     1436.1  2.317038e+09   \n",
       "4  1723532401000    1423.01  1433.0  1435.0  1412.01     1436.1  2.317038e+09   \n",
       "\n",
       "   volume  pvolume  stockStatus  ...  lastSettlementPrice  settlementPrice  \\\n",
       "0   16308  1630843            0  ...                  0.0              0.0   \n",
       "1   16308  1630843            0  ...                  0.0              0.0   \n",
       "2   16308  1630843            0  ...                  0.0              0.0   \n",
       "3   16308  1630843            0  ...                  0.0              0.0   \n",
       "4   16308  1630843            0  ...                  0.0              0.0   \n",
       "\n",
       "    pe  askPrice  bidPrice  askVol  bidVol  volRatio  speed1Min  speed5Min  \n",
       "0  0.0   1425.00   1423.01      12       2       0.0        0.0        0.0  \n",
       "1  0.0   1425.15   1423.00       4      40       0.0        0.0        0.0  \n",
       "2  0.0   1425.20   1422.99       1       2       0.0        0.0        0.0  \n",
       "3  0.0      0.00      0.00       0       0       0.0        0.0        0.0  \n",
       "4  0.0      0.00      0.00       0       0       0.0        0.0        0.0  \n",
       "\n",
       "[5 rows x 22 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt_t = datetime.fromtimestamp(dt_df['time'].iloc[0]/1000).strftime('%Y-%m-%d %H:%M:%S.%f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'15:00:01.000000'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt_t[11:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt_t[11:]>='15:00:00'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'14:59:01.000000'>='15:00:00'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt_t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "bs_level = len(dict_df[keys_[0]]['bidPrice'])\n",
    "bs_name = {'bidPrice': [], 'bidVol': [], 'askPrice': [], 'askVol': []}\n",
    "dict_bs = {}\n",
    "\n",
    "for j in bs_name.keys():\n",
    "    for i in range(1, bs_level+1):\n",
    "        level_name = j + str(i)\n",
    "        dict_bs[level_name] = dict_df[keys_[0]][j][i-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bidPrice1': 1423.01,\n",
       " 'bidPrice2': 1423.0,\n",
       " 'bidPrice3': 1422.99,\n",
       " 'bidPrice4': 0.0,\n",
       " 'bidPrice5': 0.0,\n",
       " 'bidVol1': 2,\n",
       " 'bidVol2': 40,\n",
       " 'bidVol3': 2,\n",
       " 'bidVol4': 0,\n",
       " 'bidVol5': 0,\n",
       " 'askPrice1': 1425.0,\n",
       " 'askPrice2': 1425.15,\n",
       " 'askPrice3': 1425.2,\n",
       " 'askPrice4': 0.0,\n",
       " 'askPrice5': 0.0,\n",
       " 'askVol1': 12,\n",
       " 'askVol2': 4,\n",
       " 'askVol3': 1,\n",
       " 'askVol4': 0,\n",
       " 'askVol5': 0}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_bs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time': 1723532401000,\n",
       " 'lastPrice': 1423.01,\n",
       " 'open': 1433.0,\n",
       " 'high': 1435.0,\n",
       " 'low': 1412.01,\n",
       " 'lastClose': 1436.1,\n",
       " 'amount': 2317037800.0,\n",
       " 'volume': 16308,\n",
       " 'pvolume': 1630843,\n",
       " 'stockStatus': 0,\n",
       " 'openInt': 15,\n",
       " 'transactionNum': 0,\n",
       " 'lastSettlementPrice': 0.0,\n",
       " 'settlementPrice': 0.0,\n",
       " 'pe': 0.0,\n",
       " 'askPrice': [1425.0, 1425.15, 1425.2, 0.0, 0.0],\n",
       " 'bidPrice': [1423.01, 1423.0, 1422.99, 0.0, 0.0],\n",
       " 'askVol': [12, 4, 1, 0, 0],\n",
       " 'bidVol': [2, 40, 2, 0, 0],\n",
       " 'volRatio': 0.0,\n",
       " 'speed1Min': 0.0,\n",
       " 'speed5Min': 0.0}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_df[keys_[0]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "miniqmt-demo-cptEsqYo-py3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
